﻿using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using Logic.MiniGameLogic;
using Logic.Units;

namespace GameApplication.MiniGame
{
    /// <summary>
    ///     Логика взаимодействия для PageGame.xaml
    /// </summary>
    public partial class PageGame : Page, IMiniGame
    {
        private List<IUnit> _defenders;
        private List<IUnit> _troops;
        private IMiniGame _miniGame;

        public PageGame()
        {
            InitializeComponent();
            Application.Current.Properties["GameFrame"] = GameFrame;

            EndGame += winer =>
            {
                List<IUnit> x = winer;
                MessageBox.Show(String.Format("{0}", winer[0].State));
                GameFrame.Visibility = Visibility.Collapsed;

                Frame frame = Application.Current.Properties["ModalFrame"] as Frame;
                if (frame == null) return;
                frame.Visibility = Visibility.Collapsed;
            };
        }

        private void Retreat(object sender, RoutedEventArgs e)
        {
            Close();
        }

        private void Close()
        {
            Frame frame = Application.Current.Properties["ModalFrame"] as Frame;
            if (frame == null) return;
            frame.Navigate(null);
            frame.Visibility = Visibility.Collapsed;
        }

        #region IMiniGame

        public void SetOpponents(List<IUnit> defenders, List<IUnit> troops)
        {
            _troops = troops;
            _defenders = defenders;
        }

        public event Action<List<IUnit>> EndGame;

        public void SetWiner(List<IUnit> winer)
        {
            OnEndGame(winer);
        }

        public void Start()
        {
            Frame modalFrame = Application.Current.Properties["ModalFrame"] as Frame;
            if (modalFrame == null) return;

            modalFrame.Content = this;

            modalFrame.Visibility = Visibility.Visible;
            modalFrame.Focus();
        }

        protected virtual void OnEndGame(List<IUnit> winer)
        {
            Action<List<IUnit>> handler = EndGame;
            if (handler != null) handler(winer);
        }

        #endregion

        private void StartGeme(object sender, RoutedEventArgs e)
        {
            BRetreat.Visibility = Visibility.Collapsed;
            BPlay.Visibility = Visibility.Collapsed;
            BAutomaticBattle.Visibility = Visibility.Visible;
            BSurrender.Visibility = Visibility.Visible;


            //TODO Запуск мини игры
            _miniGame = new FieldMiniGame();
            _miniGame.SetOpponents(_defenders, _troops);

            _miniGame.EndGame += OnEndGame;

            GameFrame.Content = _miniGame;
            GameFrame.Visibility = Visibility;
        }

        private void Surrender(object sender, RoutedEventArgs e)
        {
            SetWiner(_defenders);
        }

        private void AutomaticBattle(object sender, RoutedEventArgs e)
        {
            _miniGame = new Logic.MiniGameLogic.MiniGame();
            _miniGame.SetOpponents(_defenders, _troops);
            _miniGame.EndGame += OnEndGame;
            _miniGame.Start();
        }
    }
}